//
// Created by denglibin on 2021/4/2 0002.
//
#include "window.h"
#include <stdlib.h>
/**
 * 窗口大小改变时回调
 * @param window
 * @param width
 * @param height
 */
void framebuffer_size_callback(GLFWwindow *window, int width, int height){
    //改变视口大小
    glViewport(0, 0, width, height);
}
/**
 * 初始化 创建窗口
 * @return
 */
GLFWwindow * initWindow(int width, int height, const char* title){
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    GLFWwindow *window = glfwCreateWindow(width, height, title, NULL, NULL);
    if (window == NULL)
    {
        printf("Failed to create GLFW window\n");
        glfwTerminate();
        return NULL;
    }
    glfwMakeContextCurrent(window);

    //初始化GLAD库 任何的OpenGL接口调用都必须在初始化GLAD库后才可以正常访问
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        printf("Failed to initialize GLAD");
        exit(1) ;
    }
    // 注册窗口变化监听
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    // 设置视口,告诉opengl渲染窗口的尺寸大小
    glViewport(0, 0, width, height);
    return window;
}